﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Sakura.Games.StgFramework.Builder.UI
{
	/// <summary>
	/// 单个伤害类型的编辑器。
	/// </summary>
	public partial class DamageTypeItemEditor : UserControl
	{
		#region 界面状态属性


		/// <summary>
		/// 获取或设置一个值，指示界面是否处于编辑状态。
		/// </summary>
		public bool IsInEditMode
		{
			get
			{
				return (bool)GetValue(IsInEditModeProperty);
			}
			set
			{
				SetValue(IsInEditModeProperty, value);
			}
		}

		// Using a DependencyProperty as the backing store for IsInEditMode.  This enables animation, styling, binding, etc...
		/// <summary>
		/// 标识 <see cref="IsInEditMode"/> 依赖项属性。
		/// </summary>
		public static readonly DependencyProperty IsInEditModeProperty =
			DependencyProperty.Register("IsInEditMode", typeof(bool), typeof(DamageTypeItemEditor), new UIPropertyMetadata(false, OnIsInEditModePropertyChanged));

		private static void OnIsInEditModePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			var source = (DamageTypeItemEditor)d;
			source.SyncEditState();
		}

		#endregion

		/// <summary>
		/// 同步编辑状态界面。
		/// </summary>
		private void SyncEditState()
		{
			//if (this.IsInEditMode)
			//{
			//	this.EditGrid.Visibility = System.Windows.Visibility.Visible;
			//	this.ViewGrid.Visibility = System.Windows.Visibility.Collapsed;
			//}
			//else
			//{
			//	this.EditGrid.Visibility = System.Windows.Visibility.Collapsed;
			//	this.ViewGrid.Visibility = System.Windows.Visibility.Visible;
			//}
		}

		/// <summary>
		/// 初始化一个编辑器的新实例。
		/// </summary>
		public DamageTypeItemEditor()
		{
			InitializeComponent();
		}

		private void UserControl_Loaded(object sender, RoutedEventArgs e)
		{
			this.IsInEditMode = false;
			this.SyncEditState();
		}

		private void ViewGrid_MouseEnter(object sender, MouseEventArgs e)
		{
			this.ViewActioPanel.Visibility = System.Windows.Visibility.Visible;
		}

		private void ViewGrid_MouseLeave(object sender, MouseEventArgs e)
		{
			this.ViewActioPanel.Visibility = System.Windows.Visibility.Hidden;
		}

		private void OnSaveButtonClick(object sender, RoutedEventArgs e)
		{

		}

		private void OnCancelButtonClick(object sender, RoutedEventArgs e)
		{

		}

		private void OnEditCanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = true;
		}

		private void OnEditExecuted(object sender, ExecutedRoutedEventArgs e)
		{
			this.IsInEditMode = true;
		}

		private void OnSaveCanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = true;
		}

		private void OnSaveExecuted(object sender, ExecutedRoutedEventArgs e)
		{

		}

		private void OnCancelCanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = true;
		}

		private void OnCancelExecuted(object sender, ExecutedRoutedEventArgs e)
		{

		}

		#region 数据对象

		/// <summary>
		/// 获取或设置要编辑的数据项目。
		/// </summary>
		public DamageType DataItem
		{
			get
			{
				return (DamageType)GetValue(DataItemProperty);
			}
			set
			{
				SetValue(DataItemProperty, value);
			}
		}

		/// <summary>
		/// 标识 <see cref="DataItem"/> 依赖项属性。
		/// </summary>
		public static readonly DependencyProperty DataItemProperty = DependencyProperty.Register("DataItem", typeof(DamageType), typeof(DamageTypeItemEditor), new UIPropertyMetadata(null));

		#endregion

		#region 事件和引发事件的内部方法

		/// <summary>
		/// 标识 <see cref="Ok"/> 路由事件。
		/// </summary>
		public static RoutedEvent OkEvent = EventManager.RegisterRoutedEvent("Ok", RoutingStrategy.Bubble, typeof(RoutedEvent), typeof(DamageTypeItemEditor));

		/// <summary>
		/// 标识 <see cref="Cancel"/> 路由事件。
		/// </summary>
		public static RoutedEvent CancelEvent = EventManager.RegisterRoutedEvent("Cancel", RoutingStrategy.Bubble, typeof(RoutedEvent), typeof(DamageTypeItemEditor));


		/// <summary>
		/// 当用户完成编辑并确定时引发。
		/// </summary>
		public event RoutedEventHandler Ok
		{
			add
			{
				this.AddHandler(OkEvent, value);
			}
			remove
			{
				this.RemoveHandler(OkEvent, value);
			}
		}

		/// <summary>
		/// 当用户取消编辑时引发。
		/// </summary>
		public event RoutedEventHandler Cancel
		{
			add
			{
				this.AddHandler(CancelEvent, value);
			}
			remove
			{
				this.RemoveHandler(CancelEvent, value);
			}
		}

		/// <summary>
		/// 引发 <see cref="Ok"/> 事件。
		/// </summary>
		protected void RaiseOkEvent()
		{
			var e = new RoutedEventArgs(OkEvent);
			this.RaiseEvent(e);
		}

		/// <summary>
		/// 引发 <see cref="Cancel"/> 事件。
		/// </summary>
		protected void RaiseCancelEvent()
		{
			var e = new RoutedEventArgs(CancelEvent);
			this.RaiseEvent(e);
		}

		#endregion
	}
}
